import { defineStore } from 'pinia'
import { Languages } from '@/utils/global'
import { getLanguage, setLanguage } from '@/utils/cache'
import { Menus } from '@/utils/global'
import { i18n } from '@/i18n'

export const useAppStore = defineStore('app', {
	state() {
		return {
			inited: false, //初始化状态
			collapsed: false, //菜单折叠,
			isRouterAlive: true, //视图刷新
			isDarkTheme: false, //深浅色主题
			language: '', //语言
			tags: [
				{
					label: Menus[0].label,
					path: Menus[0].path
				}
			] as Tag[]
		}
	},
	getters: {
		getLanguage(state) {
			return Languages.filter((ele) => ele.id === state.language)[0]
		}
	},
	actions: {
		Init() {
			this.inited = false
			this.ChangeLanguage(getLanguage())
			this.inited = true
		},
		SwitchCollapsed() {
			this.collapsed = !this.collapsed
		},
		ChangeLanguage(language: Language) {
			this.language = language
			setLanguage(language)
			i18n.global.locale.value = language
		},
		AddTag(tag: Tag) {
			const exists = this.tags.some((ele) => ele.path === tag.path)
			if (exists) return
			this.tags.push(tag)
		},
		RemoveTag(tag: Tag) {
			const index = this.tags.findIndex((ele) => ele.path === tag.path)
			this.tags.splice(index, 1)
		}
	}
})
